package logic

import (
	"context"
	"encoding/json"
	"shuqi-novel/models/cache"
	"shuqi-novel/models/mysql"
	"strconv"
	"time"

	"shuqi-novel/shuqirpc/internal/svc"
	"shuqi-novel/shuqirpc/shuqirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type NovelListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelListLogic {
	return &NovelListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *NovelListLogic) NovelList(in *shuqirpc.NovelListRequest) (*shuqirpc.NovelListResponse, error) {
	// todo: add your logic here and delete this line

	var novel mysql.Novel
	var lists []*shuqirpc.NovelList

	key := "Novel_" + strconv.Itoa(int(in.Page))
	var err error
	if lists, err = novel.NovelList(mysql.DB, in, lists); err != nil {
		return nil, err
	}
	jsonData, _ := json.Marshal(lists)
	cache.Rdb.Set(l.ctx, key, jsonData, time.Hour*2)

	return &shuqirpc.NovelListResponse{
		List: lists,
	}, nil
}
